package Test
{
	import Interface.Common.IEngineManager;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.net.FileFilter;
	import flash.net.URLRequest;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;
	import flash.utils.ByteArray;
	import flash.utils.getQualifiedClassName;
	
	public class Test extends MovieClip
	{
		private var _gameEngine:IEngineManager;
		
		public function Test()
		{
			super();
		}
		
		private function startGame():void
		{
			
			var GameURL:String = "ClientEngine.swf";
			
			for(var t_name:* in loaderInfo.parameters)
			{
				if(GameURL == "ClientEngine.swf")
					GameURL = "ClientEngine.swf?type=1";
				GameURL = GameURL+"&"+t_name+"="+loaderInfo.parameters[t_name];
			}
			
			var loader:Loader = new Loader();
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE,loadSWFComplete);
			var loadercontext:LoaderContext = new LoaderContext(false,ApplicationDomain.currentDomain);
			this.addChild(loader);
			try
			{
				var urlReq:URLRequest = new URLRequest(GameURL);
				trace(urlReq.method);
				loader.load(urlReq, loadercontext);
			}
			catch(e:Error)
			{
				trace("Error-------");
			}
		}
		
		private function loadSWFComplete(p_e:Event):void
		{
			var swfLoader:Loader = p_e.currentTarget.loader;
			swfLoader.contentLoaderInfo.removeEventListener(Event.COMPLETE, loadSWFComplete);
			_gameEngine = p_e.currentTarget.content as IEngineManager;
			_gameEngine.initManager();
			_gameEngine.printLog(ApplicationDomain.currentDomain.getDefinition("Engine.Model.EventManager"));
			this._gameEngine.checkSharedObjectSaveSetting(onCompuleteCheck);
			function onCompuleteCheck():void
			{
				_gameEngine.loadURLResVersion(null,versionCompulete);
			}
		} 
		
		private function versionCompulete():void
		{
			_gameEngine.loadImage("config/erguang_s001.png",onCompulete);
			_gameEngine.loadSWF("config/powerOkPlayer.swf",onSwfCompulete);
			_gameEngine.loadSWF("TestBmp.swf",onLoadSwf);
			_gameEngine.startloading(); 
			
			function onCompulete(bitdata:BitmapData,path:String):void
			{
				var ba:ByteArray = new ByteArray();
				ba.writeObject(bitdata);
				ba.position = 0;
				var bmp:Bitmap = new Bitmap(bitdata);
				addChild(bmp);
				addChild(_gameEngine as DisplayObject);
			}
			
			function onSwfCompulete(evt:Event):void
			{
				var info :LoaderInfo = evt.currentTarget as LoaderInfo;
				var display:DisplayObject = info.content as DisplayObject;
				display.x = 100;
				addChild(display);
				addChild(_gameEngine as DisplayObject); 
				_gameEngine.printLog(getQualifiedClassName(info.content));
			}
			
			function onLoadSwf(evt:Event):void
			{
				var info :LoaderInfo = evt.currentTarget as LoaderInfo;
				var display:DisplayObject = info.content as DisplayObject;
				display.x = 100;
				addChildAt(display,0);
				addChild(_gameEngine as DisplayObject);
			}
		}
		
	}
}